package org.jash.sportnews2109.vm

import androidx.lifecycle.MutableLiveData
import dagger.hilt.android.lifecycle.HiltViewModel
import org.jash.common.mvvm.BaseViewModel
import org.jash.common.retrofit
import org.jash.common.token
import org.jash.sportnews2109.dao.NewsDao
import org.jash.sportnews2109.dao.UserDao
import org.jash.sportnews2109.entity.Comment
import org.jash.sportnews2109.entity.News
import org.jash.sportnews2109.entity.User
import org.jash.sportnews2109.net.APIService
import javax.inject.Inject

@HiltViewModel
class DetailViewModel @Inject constructor(val newsDao:NewsDao, val userDao: UserDao):BaseViewModel() {
    val newsLiveData by lazy { MutableLiveData<News>() }
    val collectLiveData by lazy { MutableLiveData<String>() }
    val commentsLiveData by lazy { MutableLiveData<List<Comment>>() }
    val sendCommentLiveData by lazy { MutableLiveData<String>() }
    val service by lazy { retrofit.create(APIService::class.java)}
    fun loadNews(id:Int) {
        progress(newsDao.findById(id), newsLiveData::postValue)
        if (token != null) {
            progress(service.getDetail(id)) {
                if (it.code == 0) {
                    newsLiveData.postValue(it.data)
                    newsDao.save(it.data)
                } else {
                    errorLiveData.postValue(RuntimeException(it.msg))
                }
            }
        }
    }
    fun collect(id:Int) {
        progress(service.collect(id)) {
            if (it.code == 0) {

                collectLiveData.postValue(it.data)
            } else {
                errorLiveData.postValue(RuntimeException(it.msg))
            }
        }
    }
    fun loadComments(id:Int) {
        updateUser()
        progress(service.getCommentsByNewsId(id)) {
            if (it.code == 0) {
                it.data.forEach {
//                    it.user = User(it.uid, "用户1", "https://demo2022.axureshop.com/1852265/images/%E6%96%B0%E9%97%BB%E8%AF%A6%E6%83%85%EF%BC%88%E8%A7%86%E9%A2%91_%E6%96%87%E7%AB%A0%EF%BC%89/u1861.png" )
                    it.user = userDao.findById(it.uid)
                    it.replays.forEach {
//                        it.user = User(it.uid, "回复用户", "https://demo2022.axureshop.com/1852265/images/%E6%96%B0%E9%97%BB%E8%AF%A6%E6%83%85%EF%BC%88%E8%A7%86%E9%A2%91_%E6%96%87%E7%AB%A0%EF%BC%89/u1861.png")
                        it.user = userDao.findById(it.uid)
                    }
                }
                commentsLiveData.postValue(it.data)
            } else {
                errorLiveData.postValue(RuntimeException(it.msg))
            }
        }
    }
    fun updateUser() {
        progress(service.getUsers1().zipWith(service.getUsers2()) { r1, r2 ->
            if (r1.code == 0 && r2.code == 0) {
                return@zipWith r1.data.zip(r2.data) {  u1, u2 ->
                    User(u1.id, u1.username ?: u2.username, u1.imgurl ?: u2.imgurl)
                }
            } else {
                return@zipWith listOf()
            }
        }) {
            userDao.save(*it.toTypedArray())
        }
    }
    fun sendComment(comment: Comment) {
        progress(service.sendComment(comment)) {
            if (it.code == 0) {
                sendCommentLiveData.postValue("发表评论成功")
            } else {
                errorLiveData.postValue(RuntimeException(it.msg))
            }
        }
    }
}